package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import data.User;

/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents displaying member's email information.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

public class EmailPanel extends ViewPanel
{
    private JTextArea msgField;
    private DefaultTableModel model;
    private JTable table;

    /**
     * Construct ViewPanel containing JComponents displaying member's email information
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public EmailPanel(ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        JLabel title = new JLabel("Email");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
		
		String[][] rowValues;
		String[] columnNames = {"Name", "EMail"};
		
		ArrayList<User> users = viewManager.getGs().getGroup().getUsers();
		rowValues = new String[users.size()][2];
		for(int i = 0; i < users.size(); i++)
		{
		   User u = users.get(i);
		   rowValues[i][0] = u.getRealName();
		   rowValues[i][1] = u.getEmail();
		}
		
		model = new DefaultTableModel(rowValues, columnNames);
		table = new JTable(model){  
		    public boolean isCellEditable(int row, int column){  
		        return false;  // make table uneditable
		      }  
		    };
		table.removeEditor();
		table.setSize(250,100);
		table.setLocation(100,100);
		this.add(table);		
		
		Dimension labelSize = new Dimension(80, 20);
        int labelX = table.getX();
        int labelY = table.getY() + 100;
        int labelSpread = 30;
        
        // labels        
        labelY += labelSpread;
        JLabel msgLabel = new JLabel("Message: ");
        msgLabel.setSize(labelSize);
        msgLabel.setLocation(labelX - 70, labelY);
        this.add(msgLabel);
        
        //textfield
        int textFieldX = labelX;
        int textFieldY = labelY;
        
        msgField = new JTextArea();        
        JScrollPane msgPanel = new JScrollPane(msgField);
        msgPanel.setSize(250,120);
        msgPanel.setLocation(textFieldX, textFieldY);
        labelY+= 150;
        this.add(msgPanel);
        
        //buttons
        final JButton confirmButton = new JButton("Send");
        confirmButton.setSize(80,20);
        confirmButton.setLocation(100, labelY);
        confirmButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if(model.getRowCount() != 0) //make sure list isn't empty
				{
					String memSelected = "";
					int[] selectedRows = table.getSelectedRows();
					if(selectedRows.length != 0)
					{
						for(int i = 0; i < selectedRows.length; i++)
						{
							String userName = model.getValueAt(selectedRows[i], 0).toString();
							memSelected += userName + "\n";
						}
						JOptionPane.showMessageDialog(confirmButton, "Message Sent To: " + "\n" + memSelected);
						msgField.setText("");
					}
					else
						JOptionPane.showMessageDialog(confirmButton, "No one is selected");
				}
			}
		});
        this.add(confirmButton);
        
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setSize(80,20);
        cancelButton.setLocation(200, labelY);
        cancelButton.addActionListener(viewManager.addLink("calendar"));
        this.add(cancelButton);
    }
    
    /**
     * Paints graphical aspects of the panel
     *@param g the graphical component used to paint the panel 
     */
    public void paintComponent(Graphics g)
    {
        Graphics2D g2 = (Graphics2D) g;
        
        g2.drawString("Member's Emails", 50, 80);
        
        g2.drawLine(30, 90, 500, 90);
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {       
       String[][] rowValues;
       String[] columnNames = {"Name", "EMail"};
       ArrayList<User> users = super.getViewManager().getGs().getGroup().getUsers();
       rowValues = new String[users.size()][2];
       for(int i = 0; i < users.size(); i++)
       {
          User u = users.get(i);
          rowValues[i][0] = u.getRealName();
          rowValues[i][1] = u.getEmail();
       }
       
       model = new DefaultTableModel(rowValues, columnNames);
       table.setModel(model);
       
        msgField.setText("");
        table.clearSelection();
    }
}
